<?php
class App_Image
{
	protected $_msg = '';
	protected $_dir = '';
	
	public function upload($file, $dir)
	{
		$upload = new App_File();
		$path = $upload->upload($file, $dir);
		if (!$path) {
			$this->_msg = $upload->msg();
			return false;
		}
		return $path;
	}
	
	public function makeThumb($path, $width, $height, $dir)
	{
		$gd = $this->getGd();
		if ($gd == 0) {
			$this->_msg = 'GD没检测到';
			return false;
		}
		
        $this->_dir = $dir . '/';
        
	    if (!file_exists($this->_dir)) {
            if (!App_Misc::make_dir($this->_dir)) {
                $this->_msg = '文件夹创建失败';
                return false;
            }
        }
        
        $fileName = $this->getFilename($this->checkType($path));         
        $imgInfo = @getimagesize($path);
        
        if (!$imgInfo) {
        	$this->_msg = '原始图片不存在';
        	return false;
        }
        
        $imgResource = $this->imgResource($path, $imgInfo[2]);        
        $scale = $imgInfo[0]/$imgInfo[1];
        
	    if ($width == 0) {
            $width = $height * $scale;
        }
        if ($height == 0) {
            $height = $width / $scale;
        }
        
	    if ($gd == 2) {
            $imgThumb  = imagecreatetruecolor($width, $height);
        } else {
            $imgThumb  = imagecreate($width, $height);
        }
        $bgcolor='#FFFFFF';
        $bgcolor = trim($bgcolor,"#");
        sscanf($bgcolor, "%2x%2x%2x", $red, $green, $blue);
        $clr = imagecolorallocate($imgThumb, $red, $green, $blue);
        
        imagefilledrectangle($imgThumb, 0, 0, $width, $height, $clr);       

        
        if ($gd == 2) {
            imagecopyresampled($imgThumb, $imgResource, 0, 0, 0, 0, $width, $height, $imgInfo[0], $imgInfo[1]);
        } else {
            imagecopyresized($imgThumb, $imgResource, 0, 0, 0, 0, $width, $height, $imgInfo[0], $imgInfo[1]);
        }   

	    if (function_exists('imagejpeg')) {
	       $fileName = str_replace($this->checkType($fileName), '.jpg', $fileName);
           imagejpeg($imgThumb, $this->_dir.$fileName);
        } elseif (function_exists('imagegif')){
           $fileName = str_replace($this->checkType($fileName), '.gif', $fileName);
           imagegif($imgThumb, $this->_dir.$fileName);
        } elseif (function_exists('imagepng')){
           $fileName = str_replace($this->checkType($fileName), '.png', $fileName);
           imagepng($imgThumb, $this->_dir.$fileName);
        } else{
           $this->_msg = "创建失败";
           return false;
        }    
        
        imagedestroy($imgThumb);
        imagedestroy($imgResource);
        
        //确认文件是否生成
        if (file_exists($this->_dir.$fileName)) {
            return str_replace(_WEB_DIR_, '', $this->_dir) . $fileName;
        } else {
            $this->_msg = '生成失败';
            return false;
        }        
	}
	
    public function checkType($path)
    {
        $pos = strrpos($path, '.');
        if ($pos !== false) {
            return strtolower(substr($path, $pos));
        } else {
            return '';
        }
    }
    
    public function getFilename($type)
    {
        $fileName = time().rand(10,99).$type;
        if (file_exists($this->_dir.$type)) {
            return $this->getFilename($type);
        } else {
            return $fileName;
        }
    }
    
    public function imgResource($imgFile, $mimeType)
    {
        switch ($mimeType)
        {
            case 1:
            case 'image/gif':
                $res = imagecreatefromgif($imgFile);
                break;

            case 2:
            case 'image/pjpeg':
            case 'image/jpeg':
                $res = imagecreatefromjpeg($imgFile);
                break;

            case 3:
            case 'image/x-png':
            case 'image/png':
                $res = imagecreatefrompng($imgFile);
                break;

            default:
                return false;
        }

        return $res;
    }
    
    public function getGd()
    {
        static $version = -1;

        if ($version >= 0) {
            return $version;
        }

        if (!extension_loaded('gd')) {
            $version = 0;
        } else {
            if (PHP_VERSION >= '4.3'){
                if (function_exists('gd_info')) {
                    $ver_info = gd_info();
                    preg_match('/\d/', $ver_info['GD Version'], $match);
                    $version = $match[0];
                } else {
                    if (function_exists('imagecreatetruecolor')) {
                        $version = 2;
                    } elseif (function_exists('imagecreate')){
                        $version = 1;
                    }
                }
            } else {
                if (preg_match('/phpinfo/', ini_get('disable_functions'))){                   
                    $version = 1;
                } else {
                  // 使用phpinfo函数
                   ob_start();
                   phpinfo(8);
                   $info = ob_get_contents();
                   ob_end_clean();
                   $info = stristr($info, 'gd version');
                   preg_match('/\d/', $info, $match);
                   $version = $match[0];
                }
             }
        }

        return $version;
     }
     
    public function msg()
    {
        return $this->_msg;
    }
}